<?php
include($_SERVER['DOCUMENT_ROOT']."/inc/Config.php");
include($_SERVER['DOCUMENT_ROOT']."/inc/Mysql_class.php");
include($_SERVER['DOCUMENT_ROOT']."/inc/Define.php");
error_reporting(0);
$db = new db($conf);
$cplx = isset($_GET['cplx'])?intval($_GET['cplx']):0;
$save_path = "/upload/excel/";

if(!isset($_FILES)){
	echo "<script>alert('未获取到上传文件');window.location.href='orderlist.php?cplx=".$cplx."';</script>";
	exit;
}
$cf = explode(".",$_FILES['file']['name']);
$houzhui = $cf[count($cf)-1];
$name = MD5(time());

if($houzhui!="xlsx"){ 
	echo "<script>alert('请上传xlsx文件');window.location.href='orderlist.php?cplx=".$cplx."';</script>";
	exit;
}
if (!@move_uploaded_file($_FILES['file']["tmp_name"], $_SERVER['DOCUMENT_ROOT'].$save_path.$name.".".$houzhui)) {	
	echo "<script>alert('error|上传文件错误');window.location.href='orderlist.php?cplx=".$cplx."';</script>";
	exit;	
}

/*读取excel文件，并进行相应处理*/
$fileName = $_SERVER['DOCUMENT_ROOT'].$save_path.$name.".".$houzhui;
if (!file_exists($fileName)) {    
	echo "<script>alert(文件".$fileName."不存在);window.location.href='orderlist.php?cplx=".$cplx."';</script>";
	exit;
}
$startTime = time(); //返回当前时间的Unix 时间戳
include($_SERVER['DOCUMENT_ROOT']."/inc/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php");//引入PHPExcel类库
$ex = PHPExcel_IOFactory::load($fileName);
$ex->setActiveSheetIndex(0);//默认选中sheet0表
$sheet = $ex->getActiveSheet();
$xnum = $sheet->getHighestRow();//获取表格行数
$ynum = $sheet->getHighestColumn();//获取表格列数
$data = array();
//行数循环
for ($i=2;$i<=$xnum;$i++){
	$oid = intval($sheet->getCell("A".$i)->getValue());	
	$oinfo = $db->field(array("*"))->where("Id=$oid")->select("sc_orders");
	if($oinfo && $oinfo[0]['state']==1){		
		$trackname = $sheet->getCell("L".$i)->getValue();		
		$trackcode = $sheet->getCell("M".$i)->getValue();
		if($trackname && $trackcode){
			$db->where("Id=$oid")->update("sc_orders",['state'=>2,'sendtime'=>date("Y-m-d H:i:s"),'trackname'=>$trackname,'trackcode'=>$trackcode]);
		}
	}
    
}
echo "<script>alert('更新完成');window.location.href='orderlist.php?cplx=".$cplx."';</script>";


?>